<?php

class Ead_CalendarioController extends App_Controller_Action
{  
    public $dir = 'public/upload/calendario/';
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $obj = new Ead_Model_DbTable_Calendario();
        //$result = $obj->fetchAll();
        $result = $obj->fetchAll($obj->select()->order(array('CAL_ORD ASC')));
        $this->view->result = $result;//$this->paginacao($result);
    }
    
    
    public function editAction() {
        
        $form = new Ead_Form_Calendario();
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('CAL_ID');
                
                $returnImg = imagem::upload('CAL_FILE',$this->dir,$id);
                
                $data = array(
                    'CAL_ID'    => $form->getValue('CAL_ID'),
                    'CAL_PRD'   => $form->getValue('CAL_PRD'),
                    'CAL_DAT'   => $form->getValue('CAL_DAT'),
                    'CAL_LNK'   => $form->getValue('CAL_LNK'),
                    'CAL_ORD'   => $form->getValue('CAL_ORD'),
                );
                $obj = new Ead_Model_DbTable_Calendario();

                if ($id == 0){
                    $obj->insert($data);
                    $id =  $obj->getAdapter()->lastInsertId();                    
                    imagem::rename($id,$this->dir,$returnImg['filenew']);
                }
                else
                    $obj->update($data, 'CAL_ID = ' . $id);
                
                if(strlen($_FILES['CAL_FILE']['tmp_name']) > 1){
                    $img = glob($this->dir.$id.'.*');
                    foreach($img as $filetemp) $filename = $filetemp;
                    imagem::reizeFull($filename,$filename, '100', '100');
                }                
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Calendario();
                $form->populate($obj->fetchRow('CAL_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Calendario();
            $ok = (bool) $obj->delete('CAL_ID = '.(int)$id);
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Ead_Model_DbTable_Fullbanner();
            //$form->populate($obj->fetchRow('CAL_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

